'use strict';
/**
 * logic
 * @param  {} []
 * @return {}     []
 */
export default class extends think.logic.base {
    * __before(){
        if(!(typeof this.http.data === 'object') && this.http.data){
            try {
                this.http.data = JSON.parse(this.http.data);
            } catch(e) {
                think.log(e);
                this.http.data = {};
                return ;
            }
        }

        // this.allowMethods = "post";

        this.http._post = this.http.data;

        this.http._get  = this.http.data;

        yield this._user();
   }

   //用户处理
   * _user(){
        this.http.socket.user = this.http.getUser();
        if(!this.http.socket.user){
            let User = think.service("user", "socket"); //加载 socket 模块下的 user service
        
            this.http.socket.user = new User({
                sid  : this.http.socket.id,
                ip   : this.http.socket.handshake.query.ip   || this.http.socket.handshake.address,
                nick : this.http.socket.handshake.query.nick || "游客",
                pg   : this.http.socket.handshake.query.pg   || 0,
                rg   : this.http.socket.handshake.query.rg   || 0,
                uid  : this.http.socket.handshake.query.uid  || 0,
                token: this.http.socket.handshake.query.token,
                time : new Date().getTime()
            });

            yield this.http.socket.user.check();

            this.http.updateUser(this.http.socket.user);
        }
   }

   //用户检查
   checkUser(){
      if(this.http.getUser()){
        return true;
      }
      return false;
   }
}